/*
 * Copyright (c) 2006-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.livejournal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.panteleyev.blogclient.BloggerEditorFrame;
import org.panteleyev.blogclient.IPicturePublishDialog;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SimpleComboBoxModel;
import org.panteleyev.swing.SwingFactory;

import com.google.common.collect.ImmutableList;
import javax.swing.AbstractAction;

/**
 *
 * @author  Petr Panteleyev
 */
class PicturePublishDialog extends BasicDialog<JFrame> implements IPicturePublishDialog {
    private ImmutableList<BloggerEditorFrame<?>> _wndList;
    private SimpleComboBoxModel _wndListModel;

    private JCheckBox           useTableCheckBox;
    private JRadioButton        copyToClipboardRadioButton;
    private JRadioButton        copyToWindowRadioButton;
    private JComboBox           postWindowComboBox;
    private JRadioButton        fullSizeRadioButton;
    private JRadioButton        reducedSizeRadioButton;
    private JComboBox<String>   sizeComboBox;
    private JCheckBox           linkToOriginalCheckBox;
    private JComboBox<String>   columnsComboBox;
    private JCheckBox           annotationCheckBox;
    private JCheckBox           borderCheckBox;

    /** Creates new form PicturePublishDialog */
    public PicturePublishDialog(JFrame parent) {
        super(parent);
        initComponents();

        _wndList = BloggerEditorFrame.getAllEditorFrames();
        _wndListModel = new SimpleComboBoxModel() {
            @Override
            public Object getElementAt(int index) {
                return _wndList.get(index).getTitle();
            }

            @Override
            public int getSize() { return _wndList.size(); }
        };

        postWindowComboBox.setModel(_wndListModel);

        if (_wndListModel.getSize() > 0) {
            postWindowComboBox.setSelectedIndex(0);
            copyToWindowRadioButton.setSelected(true);
        } else {
            copyToClipboardRadioButton.setSelected(true);
            copyToWindowRadioButton.setEnabled(false);
            postWindowComboBox.setEnabled(false);
        }

        fullSizeRadioButton.setSelected(true);
        enableSizeControls(false);

        pack();
    }

    private void initComponents() {
        setTitle(TITLE);
        setResizable(false);
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));

        JPanel panel;
        JPanel tablePanel;

        final AbstractAction useTableAction = new AbstractAction(USE_TABLE) {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                final boolean b = useTableCheckBox.isSelected();
                columnsComboBox.setEnabled(b);
                borderCheckBox.setEnabled(b);
                annotationCheckBox.setEnabled(b);
            }
        };

        getContentPane().add(tablePanel = SwingFactory.createPanel(
            BorderFactory.createTitledBorder(TABLE), SwingFactory.LAYOUT.BOX_Y,
            useTableCheckBox = new JCheckBox(useTableAction),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                new JLabel(COLUMNS),
                Box.createHorizontalStrut(3),
                columnsComboBox = new JComboBox<>(new String[] { "1", "2", "3", "4" }),
                Box.createHorizontalStrut(5),
                annotationCheckBox = new JCheckBox(L10N.PPD_ANNOTATION_CHECKBOX.s()),
                Box.createHorizontalStrut(5),
                borderCheckBox = new JCheckBox(BORDER),
                Box.createHorizontalGlue()
            )
        ));

        useTableCheckBox.setSelected(true);

        getContentPane().add(panel = SwingFactory.createPanel(
            BorderFactory.createTitledBorder(LINK), SwingFactory.LAYOUT.BOX_Y,
            fullSizeRadioButton = new JRadioButton(FULL_SIZE),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                reducedSizeRadioButton = new JRadioButton(REDUCED_SIZE),
                Box.createHorizontalStrut(3),
                sizeComboBox = new JComboBox<>(new String[] { "100", "150", "320", "640" }),
                Box.createHorizontalGlue()
            ),
            linkToOriginalCheckBox = new JCheckBox(LINK_TO_ORIGINAL)
//            _imgBorderCheckBox = new JCheckBox(L10N.PPD_BORDER_LABEL.s())
        ));
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);

        ButtonGroup grp = new ButtonGroup();
        grp.add(fullSizeRadioButton);
        grp.add(reducedSizeRadioButton);

        getContentPane().add(panel = SwingFactory.createPanel(
            BorderFactory.createTitledBorder(PUBLICATION), SwingFactory.LAYOUT.BOX_Y,
            copyToClipboardRadioButton = new JRadioButton(COPY_TO_CLIPBOARD),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                copyToWindowRadioButton = new JRadioButton(COPY_TO_WINDOW),
                Box.createHorizontalStrut(3),
                postWindowComboBox = new JComboBox(),
                Box.createHorizontalGlue()
            )
        ));
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);

        grp = new ButtonGroup();
        grp.add(copyToClipboardRadioButton);
        grp.add(copyToWindowRadioButton);

        fullSizeRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                enableSizeControls(false);
            }
        });

        reducedSizeRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                enableSizeControls(true);
            }
        });

        getContentPane().add(new OkCancelButtonPanel(this, JComponent.LEFT_ALIGNMENT));
        SwingFactory.setAlignmentX(tablePanel, LEFT_ALIGNMENT);
        SwingFactory.setAlignmentX(getContentPane(), LEFT_ALIGNMENT);
        pack();
    }

    int getColumnCount() {
        return Integer.parseInt((String)columnsComboBox.getSelectedItem());
    }

    boolean getFullSize() {
        return fullSizeRadioButton.isSelected();
    }

    boolean getLinkToSource() {
        return linkToOriginalCheckBox.isSelected();
    }

    String getSmallSize() {
        return (String)sizeComboBox.getSelectedItem();
    }

    boolean getAnnotate() {
        return annotationCheckBox.isSelected();
    }

    boolean getBorder() {
        return borderCheckBox.isSelected();
    }

    private void enableSizeControls(boolean enable) {
        sizeComboBox.setEnabled(enable);
        linkToOriginalCheckBox.setEnabled(enable);
    }

    BloggerEditorFrame<?> getDestinationWindow() {
        if (copyToClipboardRadioButton.isSelected()) {
            return null;
        } else {
            return _wndList.get(postWindowComboBox.getSelectedIndex());
        }
    }

    public boolean isUseTable() {
        return useTableCheckBox.isSelected();
    }
}
